<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_manage extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('user_model');
	}

	public function index()
	{
		//get获取查询参数
		$page = intval($this->input->get('page', true)); //页数
		$where['groupid'] = $this->input->get('groupid', TRUE);
		$where['uid'] = $this->input->get('uid', TRUE);
		$where['username'] = $this->input->get('username', TRUE);
		$where['email'] = $this->input->get('email', TRUE);
		$where['like'] = $this->input->get('like', TRUE);

		//分页
		$count = $this->user_model->count_all_list($where); //查询总数
		$config['base_url'] = site_url('admin/user_manage/index?'); //基本url
		$config['total_rows'] = $count; //总数据条数
		$config['per_page'] = 10; //每页显示多少条数据
		$this->load->library('pagination', $config);

		//根据条件查询需要显示的数据
		$users = array();
		if ($count)
		{
			$limit = $config['per_page']; //$this->pagination->per_page;
			$offset = $this->pagination->get_offset($page);
			$users = $this->user_model->get_all_list($where, $limit, $offset, 'uid DESC');
		}
		$data['users'] = $users;
		$data['get'] = $where;
		$this->load->helper('form');
		$this->load->view('admin/user_manage_index', $data);
	}

	public function add()
	{
		$this->load->helper('form');
		$this->load->model('user_group_model', 'group_model');

		$data['groups'] = $this->group_model->get_all();
		$this->load->view('admin/user_manage_add', $data);
	}

	public function do_add()
	{
		$this->load->library('form_validation');

		$groupid = intval($this->input->post('groupid', TRUE));
		$username = trim($this->input->post('username', TRUE));
		$password = trim($this->input->post('password', TRUE));
		//$repassword = $this->input->post('repassword', TRUE);
		$email = trim($this->input->post('email', TRUE));

		if ($username == '')
		{
			show_admin_msg('用户名不能为空');
		}
		if ($password == '')
		{
			show_admin_msg('密码不能为空');
		}
		if ($email == '')
		{
			show_admin_msg('邮箱不能为空');
		}
		if ($this->form_validation->matches($password, 'repassword') == FALSE)
		{
			show_admin_msg('两次输入的密码不一致');
		}
		if ($this->user_model->check_username($username) == FALSE)
		{
			show_admin_msg('用户名已经存在');
		}
		if ($this->form_validation->valid_email($email) == FALSE)
		{
			show_admin_msg('非法邮箱地址');
		}
		if ($this->user_model->check_email($email) == FALSE)
		{
			show_admin_msg('邮箱地址已经存在');
		}

        $insert['groupid'] = $groupid;
        $insert['username'] = $username;
        $insert['password'] = $password;
        $insert['email'] = $email;

        $this->user_model->create_user($insert);

        redirect('admin/user_manage/index');
	}

	public function edit_base()
	{
		$uid = intval($this->input->get('uid', TRUE));
		$user = $this->user_model->get($uid);

		$data['uid'] = $uid;
		$data['user'] = $user;
		$this->load->helper('form');
		$this->load->view('admin/user_manage_edit_base', $data);
	}

	public function do_edit_base()
	{
		$this->load->library('Form_validation');

		$post = $this->input->post(null, TRUE);

		$update['uid'] = intval($post['uid']);
		$update['username'] = trim($post['username']);
		$update['password'] = trim($post['password']);
		if ($update['password'] == '')
		{
			unset($update['password']);
		}
		$update['created'] = trim($post['created']);
		$update['created'] = strtotime($update['created']);
		$update['created_ip'] = trim($post['created_ip']);
		$update['created_ip'] = sprintf('%u', ip2long($update['created_ip']));
		$update['realname'] = trim($post['realname']);
		$update['gender'] = intval($post['gender']);
		$update['alipay'] = trim($post['alipay']);
		$update['mobile'] = trim($post['mobile']);
		$update['telphone'] = trim($post['telphone']);
		$update['address'] = trim($post['address']);
		$update['email'] = trim($post['email']);
		$update['qq'] = $post['qq'];


		if ($this->user_model->check_username($post['username'], $update['uid']) == FALSE)
		{
			show_admin_msg('用户名已经存在');
		}
		$valid_email = $this->form_validation->valid_email($post['email']);
		$valid_email == FALSE && show_admin_msg('非法邮箱地址');
		if ($this->user_model->check_email($post['email'], $update['uid']) == FALSE)
		{
			show_admin_msg('邮箱地址已经存在');
		}

		$this->user_model->update($update);

		redirect('admin/user_manage/edit_base?uid=' . $update['uid']);
	}

	public function edit_data()
	{
		$uid = intval($this->input->get('uid', TRUE));
		$user = $this->user_model->get_data_and_user($uid);

		$data['uid'] = $uid;
		$data['user'] = $user;
		$this->load->helper('form');
		$this->load->view('admin/user_manage_edit_data', $data);
	}

	public function do_edit_data()
	{
		$this->load->library('Form_validation');

		$post = $this->input->post(null, TRUE);

		$update_data = $post;
		$update_data['lastsaletime'] = strtotime($post['lastsaletime']);
		$update_data['lasthiretime'] = strtotime($post['lasthiretime']);

		$this->user_model->update_data($update_data);

		redirect('admin/user_manage/edit_data?uid=' . $update_data['uid']);
	}

	public function change_status()
	{
		$uid = $this->input->get('uid', TRUE);
		$status = $this->input->get('status', TRUE);

		$user = $this->user_model->get($uid);

		if ($user)
		{
			if ($status == 'lock')
			{
				$this->user_model->lock($uid);
				show_admin_msg('用户锁定成功', 'success');
			}
			elseif ($status == 'active')
			{
				$this->user_model->active($uid);
				show_admin_msg('用户激活成功', 'success');
			}
			elseif ($status == 'unlock')
			{
				$this->user_model->unlock($uid);
				show_admin_msg('用户解锁成功', 'success');
			}
		}
		else
		{
			show_admin_msg('用户不存在或已删除');
		}

	}
}

/* End of file user_manage.php */
/* Location: ./application/controllers/admin/user_manage.php */
